//
//  BLCommendVerticalCell.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/5/28.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import UIKit
import SDWebImage


class BLCommendVerticalCell: UITableViewCell {
    
    
    //房间01属性
    @IBOutlet weak var roomBtn01: BLVerticalRoomSingleButton!
    //房间02属性
    @IBOutlet weak var roomBtn02: BLVerticalRoomSingleButton!
    //房间03属性
    @IBOutlet weak var roomBtn03: BLVerticalRoomSingleButton!
    //房间04属性
    @IBOutlet weak var roomBtn04: BLVerticalRoomSingleButton!
    //房间数组
    lazy var roomButtonArray = [BLVerticalRoomSingleButton]()
    
    //网络数据数组
    lazy var verticalRoomArray : [BLDouyuRoomDetalModal] = [BLDouyuRoomDetalModal]()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        roomButtonArray.append(roomBtn01)
        roomButtonArray.append(roomBtn02)
        roomButtonArray.append(roomBtn03)
        roomButtonArray.append(roomBtn04)
        
        
        //请求网络数据
                getVerticalRoomData()
        //配置cell属性
        self.autoresizingMask = UIViewAutoresizing.None;
        self.selectionStyle = UITableViewCellSelectionStyle.None;
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}

/// MARK: - 请求网络数据
extension BLCommendVerticalCell{
    //获取颜值room数据
    func getVerticalRoomData(){

        BLGetDouyuData.shareInstance.getVerticalRoomData { (dataArray) in
            self.verticalRoomArray = dataArray
            //设置与刷新视图
            self.setupUI()
            self.setNeedsDisplay()
        }

    }
}

// MARK: - UI配置
extension BLCommendVerticalCell{
    
    
    func setupUI() {
        //数组校验
        guard verticalRoomArray.count != 0 else{
            return
        }
        guard roomButtonArray.count != 0 else{
            return
        }
        
        for i in 0..<4 {
            let item = verticalRoomArray[i]
            let roomBtn = roomButtonArray[i]
            
            roomBtn.setupRoomData(onlineNumber: item.online, userName: item.nickname,roomID: item.room_id)
            
            roomBtn.sd_setBackgroundImageWithURL(NSURL(string:item.vertical_src!), forState: .Normal)
            roomBtn.addTarget(self, action: #selector(roomBtnClick(_:)), forControlEvents: .TouchUpInside)
        }
        
    }
    
}

// MARK: - 响应时间配置
extension BLCommendVerticalCell{
    
    @IBAction func getMoreButton(sender: UIButton) {
        //TODO:BLCommendVerticalCell 更多按钮
        print("BLCommendVerticalCell 更多按钮")
    }
    
    func roomBtnClick(btn : BLVerticalRoomSingleButton) {
        print(btn.roomID)
        /// 通知移除
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
}